/*********************************
File name:  essmanualworker1-10.do
DATE:		3/2024
PURPOSE:    This do file creates the ESS parts of Figure 2 and 3 
            (Fig2: Manual workers support for SD (RR) and 
			 the composition of SD (RR) over time; Fig3: 
			 manual workers support for other party families)
            It is organized in the following way:
			1. Creates occupational categorization by sector
			2. Creates ESS part of Fig. 2a (Rate of support for SD)
			3. Creates ESS part of Fig. 2b (Composition of SD)
			4. Creates ESS part of Fig. 2c (Rate of support for the RR)
			5. Creates ESS part of Fig. 2d (Composition of the RR)
			6. Creates ESS part of Conservative rate of support
			7. Creates ESS part of CD rate of support
			8. Creates ESS part of RL rate of support
			9. Creates ESS part of LIB rate of support
			10.Creates ESS part of GRN rate of support
			11.Combines the EB and ESS parts to form Figures 2 and 3
INPUT: 		ESS1-10_modified.dta 
LOG FILE:   essmanualworker.log
AUTHOR:     Orit Kedar, Lotem Halevy, Odelia Oshri
*********************************/
use "C:\Users\User/documents/ESS1-10_modified.dta", clear
capture log using "essmanualworker.log", replace
cd "C:\Users\User\Desktop\Figures"

set scheme s2mono
* Rename male 
rename gndr male 
tab male

/* Part 1: Define occupational categorization by sector.Manual workers = sectors 7,8,9
 craft and related trades, plant and machine operators, elementary occupation */
gen mnlwrk=.
replace mnlwrk=0 if (iscoco<6000 & iscoco>=1000) |  (isco08<6000 & isco08>=1000)        
replace mnlwrk=1 if  (iscoco<=9999 & iscoco>=7000) | (isco08<=9999 & isco08>=7000)
tab mnlwrk, miss    /*mnlwrk is 22% of sample (including missing)*/
label define mnlwrk 0 "not manual workers" 1 "manual workers", replace
label values mnlwrk mnlwrk
bys cntryear: tab mnlwrk /*min in CH18 (13%); max in PT06 (52%), mean (~25%)*/
by male, sort: tab mnlwrk /*34% of men and 16% of women are manual workers, this information is reported in the paper*/ 


* Part 2: ESS part of Fig. 2a (Rate of support for SD) 
* Numerator: Number of manual workers M/F + general sample voting SD
gen mwmsd=1 if (mnlwrk==1 & male==1 & prtyfam_sd==1)
gen mwfsd=1 if (mnlwrk==1 & male==0 & prtyfam_sd==1)
by  year cntry, sort: egen nmwmsd=total(mwmsd)   if mwmsd==1
by  year cntry, sort: egen nmwfsd=total(mwfsd)   if mwfsd==1
by  year cntry, sort: egen nsdv=total(prtyfam_sd)  if prtyfam_sd==1

* Denominator: Number of manual workers M/F who voted and number of voters
gen mwmv=1 if (mnlwrk==1 & vote==1 & male==1)
by year cntry, sort: egen nmwmv=total(mwmv) if mwmv==1
gen mwfv=1 if (mnlwrk==1 & vote==1 & male==0)
by year cntry, sort: egen nmwfv=total(mwfv) if mwfv==1
by year cntry, sort: egen nv=total(vote) 

*Proportions
gen mwmsd_mwmv 	=nmwmsd/nmwmv
gen mwfsd_mwfv   =nmwfsd/nmwfv
gen sdv_v     =nsdv/nv


/* Figure 2a: ESS part 
   Male and female manual workers support for SD *
* generate % male manual workers per country*/
by essround cntry, sort: egen mwmsdcntry=max(mwmsd_mwmv)
egen cntryuniquemw=tag(mwmsdcntry)
bys essround cntry:egen cntryunique=sum(mwmsdcntry) if cntryuniquemw==1
* generate % female manual workers per country*/
by essround cntry, sort: egen mwfsdcntry=max(mwfsd_mwfv)
egen cntryuniquemwf=tag(mwfsdcntry)
bys essround cntry:egen cntryunique1=sum(mwfsdcntry) if cntryuniquemwf==1
* generate % SD voters per country*/
by essround cntry, sort: egen sdv=max(sdv_v)
egen cntryuniquelv=tag(sdv)
bys essround cntry:egen cntryunique2=sum(sdv) if cntryuniquelv==1


/*APPENDIX: Figure D3 */
twoway lpolyci cntryunique2 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.8) acolor(538rs9%50) alsty(nono) level(95)  ///
|| scatter sdv_v year if cntryrr==1, jitter(3) graphregion(color(white))  ylabel(,nogrid) msymbol(i) mlabel(cntry) mlabsize(small) xscale(range(2002(5)2016)) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique1 year if cntryrr==1, degree(2)lcol(black) fcolor(black*.7) acolor(538rs9%50) level(95) alsty(nono)lwidth(thick)lpattern(solid) ytitle("")  ///
|| scatter mwfsd_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique year if cntryrr==1,  degree(2) alsty(nono)lcol(red) fcolor(red*.7) acolor(538rs9%50)  ///
level(95) lwidth(thick) lpattern(solid) legend(off) ///
|| scatter mwmsd_mwmv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) caption("Source: ESS 1-10") ysc(off) ///
                                         mlabcolor(red) xtitle("") 
graph save "ESS 2a", replace

*MAIN TEXT: Figure 2a: simplified version of the graph
twoway lpolyci cntryunique2 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.8) acolor(538rs9%50) alsty(nono)  graphregion(color(white))  ylabel(,nogrid)level(95) fxsize(67)||					lpolyci cntryunique1 year if cntryrr==1, degree(2)lcol(black) fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) alsty(nono)  lpattern(solid) ytitle("")  || lpolyci cntryunique year if cntryrr==1,  degree(2) lcol(red) fcolor(red*.7) alsty(nono) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("")  caption("Source: ESS 1-10") ysc(off)
graph save "ESS 2a_simple", replace


/* Part 3: ESS part for Fig. 2c (Composition of SD) */
* Numerator: Number of manual workers M/F + general sample voting SDPs
   *  nmwmsd, nmwfsd (created in Part 2)

* Denominator: Number of  M/F who voted SD
gen sdm=1 if (prtyfam_sd==1 & male==1)
by  year cntry, sort: egen nsdm=total(sdm) if sdm==1
gen sdf=1 if (prtyfam_sd==1 & male==0)
by  year cntry, sort: egen nsdf=total(sdf) if sdf==1

*Proportions
gen     mwmsd_sdm = nmwmsd/nsdm
gen     mwfsd_sdf = nmwfsd/nsdf

/* Figure 2c: ESS part 
   Male and female manual workers support for SD from all male/female left voters * 

* generate % male manual workers per country*/
by essround cntry, sort: egen mwmcntry=max(mwmsd_sdm)
egen cntryuniquemwm=tag(mwmcntry)
bys essround cntry:egen cntryunique3=sum(mwmcntry) if cntryuniquemwm==1
* generate % female manual workers per country*/
by essround cntry, sort: egen mwfcntry=max(mwfsd_sdf)
egen cntryuniquemwf1=tag(mwfcntry)
bys essround cntry:egen cntryunique4=sum(mwfcntry) if cntryuniquemwf1==1

/*APPENDIX: Figure D3c*/
twoway lpolyci cntryunique3 year if cntryrr==1, degree(2) lcolor(red) lwidth(thick) fcolor(red*.7) acolor(red9%50) alsty(nono)level(95)  ///
|| scatter mwmsd_sdm year if cntryrr==1, jitter(3) msymbol(i) graphregion(color(white)) ylabel(,nogrid) ysc(off) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(red) fxsize(67) || ///										 
lpolyci cntryunique4 year if cntryrr==1, degree(2)lcol(black) alsty(nono)fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfsd_sdf year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) caption(Source: ESS 1-10)  ///
                                         mlabcolor(black) xtitle("") legend(off) 
graph save "ESS 2c", replace

*MAIN TEXT: Figure 2c*/
twoway lpolyci cntryunique3 year if cntryrr==1, degree(2) lcolor(red) lwidth(thick) alsty(nono) fcolor(red*.7) acolor(red9%50) level(95)  fxsize(67) caption(Source: ESS 1-10) || ///										 
lpolyci cntryunique4 year if cntryrr==1, degree(2)lcolor(black) fcolor(black*.7) acolor(538rs9%50) alsty(nono) level(95) graphregion(color(white)) ylabel(,nogrid) ysc(off) lwidth(thick) lpattern(solid) ytitle("") legend(off) xtitle("")
graph save "ESS 2c_simple", replace

/* Part 4: ESS part of Fig. 2b (Rate of support for the RR) */
* Numerator: Number of manual workers M/F + general sample voting RR
gen mwmrr=1 if (mnlwrk==1 & male==1 & prtyfam_rr==1)
gen mwfrr=1 if (mnlwrk==1 & male==0 & prtyfam_rr==1)
by  year cntry, sort: egen nmwmrr=total(mwmrr)   if mwmrr==1
by  year cntry, sort: egen nmwfrr=total(mwfrr)   if mwfrr==1
by  year cntry, sort: egen nrrv  =total(prtyfam_rr)      if prtyfam_rr==1

* Denominator: Number of manual workers M/F who voted and number of voters 
*  Created in Part 3: nmwmv, nmwfv, nv 

*Proportions
gen mwmrr_mwmv 	 =nmwmrr/nmwmv
gen mwfrr_mwfv   =nmwfrr/nmwfv
gen rrv_v        =nrrv/nv

* generate % male manual workers per country voting RR*/
by essround cntry, sort: egen mwmcntryR=max(mwmrr_mwmv)
egen cntryuniquemwmR=tag(mwmcntryR)
bys essround cntry:egen cntryunique5=sum(mwmcntryR) if cntryuniquemwmR==1
* generate % female manual workers per country*/
by essround cntry, sort: egen mwfcntryR=max(mwfrr_mwfv)
egen cntryuniquemwfR=tag(mwfcntryR)
bys essround cntry:egen cntryunique6=sum(mwfcntryR) if cntryuniquemwfR==1
* generate % rr voters per country*/
by essround cntry, sort: egen rrv=max(rrv_v)
egen cntryuniquerv=tag(rrv)
bys essround cntry:egen cntryunique7=sum(rrv) if cntryuniquerv==1

/*APPENDIX Figure D3b*/
twoway lpolyci cntryunique7 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) acolor(538rs9%50) alsty(nono)level(95)  ///
|| scatter rrv_v year if cntryrr==1, jitter(3) msymbol(i) graphregion(color(white)) ylabel(,nogrid) ysc(off) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique6 year if cntryrr==1, degree(2)lcol(black) alsty(nono)fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfrr_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique5 year if cntryrr==1,  degree(2) lcol(red) alsty(nono)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) caption( Source: ESS 1-10)legend(off) ///
|| scatter mwmrr_mwmv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(red) xtitle("") 
graph save "ESS 2b", replace

/*MAIN TEXT Figure 2b*/
twoway lpolyci cntryunique7 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) alsty(nono) acolor(538rs9%50) caption(Source: ESS 1-10)level(95) fxsize(67) || lpolyci cntryunique6 year if cntryrr==1, degree(2)lcol(black) fcolor(black*.7) acolor(538rs9%50) alsty(nono) level(95) lwidth(thick) lpattern(solid) ytitle("")  || lpolyci cntryunique5 year if cntryrr==1,  degree(2) lcol(red) alsty(nono) graphregion(color(white)) ylabel(,nogrid) ysc(off)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("")ysc(off)
graph save "ESS 2b_simple" , replace

/* Part 5: ESS part of Fig. 2d (Composition of the RR) */
* Numerator: Number of manual workers M/F + general sample voting RR
  * mwmrr, mwfrr (created in Part 4)

  * Denominator: Number of  M/F who voted rr
gen rrm=1 if (prtyfam_rr==1 & male==1)
by  year cntry, sort: egen nrrm=total(rrm) if rrm==1
gen rrf=1 if (prtyfam_rr==1 & male==0)
by  year cntry, sort: egen nrrf=total(rrf) if rrf==1

*Proportions
gen     mwmrr_rrm = nmwmrr/nrrm
gen     mwfrr_rrf = nmwfrr/nrrf

* generate % male manual workers voting RR per country*/
by essround cntry, sort: egen mwmrcntry=max(mwmrr_rrm)
egen cntryuniquemwmr=tag(mwmrcntry)
bys essround cntry:egen cntryunique8=sum(mwmrcntry) if cntryuniquemwmr==1
* generate % female manual workers voting RR per country*/
by essround cntry, sort: egen mwfrcntry=max(mwfrr_rrf)
egen cntryuniquemwfr=tag(mwfrcntry)
bys essround cntry:egen cntryunique9=sum(mwfrcntry) if cntryuniquemwfr==1

/*APPENDIX: Figure D3d*/
twoway lpolyci cntryunique8 year if cntryrr==1, degree(3) lcolor(red) lwidth(thick) fcolor(red*.7) acolor(red9%50) alsty(nono)level(95)  ///
|| scatter mwmrr_rrm year if cntryrr==1, jitter(3) caption(Source: ESS 1-10)msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(red) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique9 year if cntryrr==1, degree(3)lcol(black)alsty(nono) fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfrr_rrf year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small)graphregion(color(white)) ylabel(,nogrid) ysc(off)  ///
                                         mlabcolor(black) xtitle("") legend(off) 
graph save "ESS 2d", replace

/*MAIN TEXT: Figure 2d*/
twoway lpolyci cntryunique8 year if cntryrr==1, degree(3) lcolor(red) lwidth(thick) fcolor(red*.7) acolor(red9%50) alsty(nono) level(95) fxsize(67) || 			lpolyci cntryunique9 year if cntryrr==1, degree(3)lcol(black) fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) alsty(nono)lpattern(solid) ytitle("") legend(off) xtitle("") caption(Source: ESS 1-10) graphregion(color(white)) ylabel(,nogrid) ysc(off) 
graph save "ESS 2d_simple", replace

/* Part 6: ESS panel for Rate of support for the Conservative party family */ 
* Numerator: Number of manual workers M/F + general sample voting cons.
gen mwmcon=1 if (mnlwrk==1 & male==1 & prtyfam_cons==1)
gen mwfcon=1 if (mnlwrk==1 & male==0 & prtyfam_cons==1)
by  year cntry, sort: egen nmwmcon=total(mwmcon)   if mwmcon==1
by  year cntry, sort: egen nmwfcon=total(mwfcon)   if mwfcon==1
by  year cntry, sort: egen nconv  =total(prtyfam_cons)      if prtyfam_cons==1

* Denominator: Number of manual workers M/F who voted and number of voters 
*  Created in Part : nmwmv, nmwfv, nv 

*Proportions
gen mwmcon_mwmv   =nmwmcon/nmwmv
gen mwfcon_mwfv   =nmwfcon/nmwfv
gen conv_v        =nconv/nv

* generate % male manual workers per country voting cons.*/
by essround cntry, sort: egen mwmcntrycon=max(mwmcon_mwmv)
egen cntryuniquemwmC=tag(mwmcntrycon)
bys essround cntry:egen cntryunique10=sum(mwmcntrycon) if cntryuniquemwmC==1
* generate % female manual workers per country voting cons.*/
by essround cntry, sort: egen mwfcntryC=max(mwfcon_mwfv)
egen cntryuniquemwfC=tag(mwfcntryC)
bys essround cntry:egen cntryunique11=sum(mwfcntryC) if cntryuniquemwfC==1
* generate % cons. voters per country*/
by essround cntry, sort: egen conv=max(conv_v)
egen cntryuniquecv=tag(conv)
bys essround cntry:egen cntryunique12=sum(conv) if cntryuniquecv==1

/*APPENDIX: Figure D4*/
twoway lpolyci cntryunique12 year if cntryrr==1, degree(3) alsty(nono)lcolor(gs8) lwidth(thick) fcolor(gs10*.7) acolor(538rs9%50) level(95)  ///
|| scatter conv_v year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique11 year if cntryrr==1, degree(3)lcol(black) alsty(nono)fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfcon_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique10 year if cntryrr==1,  degree(3) alsty(nono)lcol(red)  graphregion(color(white)) ylabel(,nogrid) ysc(off)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) ///
|| scatter mwmcon_mwmv year if cntryrr==1, jitter(3) caption(Source: ESS 1-10) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(red) xtitle("") 
graph save "ESS conservative", replace

/*MAIN TEXT: Figure3*/
twoway lpolyci cntryunique12 year if cntryrr==1, degree(3) lcolor(gs8) lwidth(thick)lwidth(thick) fcolor(gs10*.7) acolor(538rs9%50)  alsty(nono) level(95) fxsize(67) || lpolyci cntryunique11 year if cntryrr==1, degree(3)lcol(black) fcolor(211rs9%50) acolor(538rs9%50)  alsty(nono) level(95) lwidth(thick) lpattern(solid) ytitle("")  || lpolyci cntryunique10 year if cntryrr==1,  degree(3) lcol(red)   fcolor(red*.7) acolor(538rs9%50) alsty(nono)  caption(Source: ESS 1-10) graphregion(color(white)) ylabel(,nogrid) ysc(off) ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("")
graph save "ESS conservative_simple" replace

/* Part 7: ESS panel for Rate of support for the CD party family */
* Numerator: Number of manual workers M/F + general sample voting CD
gen mwmcd=1 if (mnlwrk==1 & male==1 & prtyfam_cd==1)
gen mwfcd=1 if (mnlwrk==1 & male==0 & prtyfam_cd==1)
by  year cntry, sort: egen nmwmcd=total(mwmcd)           if mwmcd==1
by  year cntry, sort: egen nmwfcd=total(mwfcd)           if mwfcd==1
by  year cntry, sort: egen ncdv  =total(prtyfam_cd)      if prtyfam_cd==1

* Denominator: Number of manual workers M/F who voted and number of voters 
*  Created in Part 3: nmwmv, nmwfv, nv 

*Proportions
gen mwmcd_mwmv   =nmwmcd/nmwmv
gen mwfcd_mwfv   =nmwfcd/nmwfv
gen cdv_v        =ncdv/nv

* generate % male manual workers per country voting cons.*/
by essround cntry, sort: egen mwmcntrycd=max(mwmcd_mwmv)
egen cntryuniquemwmcd=tag(mwmcntrycd)
bys essround cntry:egen cntryunique13=sum(mwmcntrycd) if cntryuniquemwmcd==1
* generate % female manual workers per country*/
by essround cntry, sort: egen mwfcntrycd=max(mwfcd_mwfv)
egen cntryuniquemwfcd=tag(mwfcntrycd)
bys essround cntry:egen cntryunique14=sum(mwfcntrycd) if cntryuniquemwfcd==1
* generate % rr voters per country*/
by essround cntry, sort: egen cdv=max(cdv_v)
egen cntryuniquecdv=tag(cdv)
bys essround cntry:egen cntryunique15=sum(cdv) if cntryuniquecdv==1
/*APPENDIX: Figure D4*/
twoway lpolyci cntryunique15 year if cntryrr==1, degree(3) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) alsty(nono)acolor(538rs9%50) level(95)  ///
|| scatter cdv_v year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique14 year if cntryrr==1, degree(3)lcol(black)alsty(nono) fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfcd_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique13 year if cntryrr==1,  degree(3)alsty(nono) lcol(red)  graphregion(color(white)) ylabel(,nogrid) ysc(off)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) caption(Source: ESS 1-10) ///
|| scatter mwmcd_mwmv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(red) xtitle("") 

graph save "ESS CD", replace

/*MAIN TEXT: Figure 3*/
twoway lpolyci cntryunique15 year if cntryrr==1, degree(3) lcolor(gs8) lwidth(thick) alsty(nono) fcolor(gs10*.7) acolor(538rs9%50) level(95) fxsize(67) || lpolyci cntryunique14 year if cntryrr==1, degree(3)lcol(black) fcolor(black*.7) alsty(nono) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  || lpolyci cntryunique13 year if cntryrr==1,  degree(3) lcol(red) lwidth(thick) alsty(nono) fcolor(red*.7) acolor(538rs9%50)caption(Source: ESS 1-10) graphregion(color(white)) ylabel(,nogrid) ysc(off)  ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("")
graph save "ESS CD_simple" replace

/* Part 8: ESS panel for Rate of support for the RL party family */
* Numerator: Number of manual workers M/F + general sample voting RL
gen mwmrl=1 if (mnlwrk==1 & male==1 & prtyfam_radical==1)
gen mwfrl=1 if (mnlwrk==1 & male==0 & prtyfam_radical==1)
by  year cntry, sort: egen nmwmrl=total(mwmrl)           if mwmrl==1
by  year cntry, sort: egen nmwfrl=total(mwfrl)           if mwfrl==1
by  year cntry, sort: egen nrlv  =total(prtyfam_radical)      if prtyfam_radical==1

* Denominator: Number of manual workers M/F who voted and number of voters 
*  Created in Part 3: nmwmv, nmwfv, nv 

*Proportions
gen mwmrl_mwmv   =nmwmrl/nmwmv
gen mwfrl_mwfv   =nmwfrl/nmwfv
gen rlv_v        =nrlv/nv

* generate % male manual workers per country voting RL.*/
by essround cntry, sort: egen mwmcntryrl=max(mwmrl_mwmv)
egen cntryuniquemwmrl=tag(mwmcntryrl)
bys essround cntry:egen cntryunique16=sum(mwmcntryrl) if cntryuniquemwmrl==1
* generate % female manual workers per country*/
by essround cntry, sort: egen mwfcntryrl=max(mwfrl_mwfv)
egen cntryuniquemwfrl=tag(mwfcntryrl)
bys essround cntry:egen cntryunique17=sum(mwfcntryrl) if cntryuniquemwfrl==1
* generate % rr voters per country*/
by essround cntry, sort: egen rlv=max(rlv_v)
egen cntryuniquerlv=tag(rlv)
bys essround cntry:egen cntryunique18=sum(rlv) if cntryuniquerlv==1

/*APPENDIX: Figure D4*/
twoway lpolyci cntryunique18 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) alsty(nono) acolor(538rs9%50) level(95)  ///
|| scatter rlv_v year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique17 year if cntryrr==1, degree(2)lcol(black) alsty(nono)fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfrl_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique16 year if cntryrr==1,  degree(2) lcol(red)  alsty(nono) fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) ///
|| scatter mwmrl_mwmv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) graphregion(color(white)) caption(Source: ESS 1-10) ylabel(,nogrid) ysc(off) ///
                                         mlabcolor(red) xtitle("") 
graph save "ESS RL", replace

/*MAIN TEXT: Figure 3*/
twoway lpolyci cntryunique18 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) alsty(nono)  acolor(538rs9%50) level(95) fxsize(67) || lpolyci cntryunique17 year if cntryrr==1, degree(2)lcol(black) fcolor(black*.7) alsty(nono) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  || lpolyci cntryunique16 year if cntryrr==1,  degree(2) lcol(red) alsty(nono)   fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("")caption(Source: ESS 1-10) graphregion(color(white)) ylabel(,nogrid) ysc(off) 
graph save "ESS RL_simple" replace 

/* Part 9: ESS panel for Rate of support for the LIB party family */
* Numerator: Number of manual workers M/F + general sample voting LIB
gen mwmlib=1 if (mnlwrk==1 & male==1 & prtyfam_lib==1)
gen mwflib=1 if (mnlwrk==1 & male==0 & prtyfam_lib==1)
by  year cntry, sort: egen nmwmlib=total(mwmlib)           if mwmlib==1
by  year cntry, sort: egen nmwflib=total(mwflib)           if mwflib==1
by  year cntry, sort: egen nlibv  =total(prtyfam_lib)      if prtyfam_lib==1

* Denominator: Number of manual workers M/F who voted and number of voters 
*  Created in Part 3: nmwmv, nmwfv, nv 

*Proportions
gen mwmlib_mwmv   =nmwmlib/nmwmv
gen mwflib_mwfv   =nmwflib/nmwfv
gen libv_v        =nrlv/nv

* generate % male manual workers per country voting LIB.*/
by essround cntry, sort: egen mwmcntrylib=max(mwmlib_mwmv)
egen cntryuniquemwmlib=tag(mwmcntrylib)
bys essround cntry:egen cntryunique19=sum(mwmcntrylib) if cntryuniquemwmlib==1
* generate % female manual workers per country voting LIB.*/
by essround cntry, sort: egen mwfcntrylib=max(mwflib_mwfv)
egen cntryuniquemwflib=tag(mwfcntrylib)
bys essround cntry:egen cntryunique20=sum(mwfcntrylib) if cntryuniquemwflib==1
* generate % LIB voters per country*/
by essround cntry, sort: egen libv=max(libv_v)
egen cntryuniquerlib=tag(libv)
bys essround cntry:egen cntryunique21=sum(libv) if cntryuniquerlib==1

/*APPENDIX: Figure D4*/
twoway lpolyci cntryunique21 year if cntryrr==1, degree(3)alsty(nono) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) acolor(538rs9%50) level(95)  ///
|| scatter libv_v year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique20 year if cntryrr==1, degree(3)lcol(black) alsty(nono)fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwflib_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique19 year if cntryrr==1,  degree(3) lcol(red) alsty(nono)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) ///
|| scatter mwmlib_mwmv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small)graphregion(color(white)) caption(Source: ESS 1-10)ylabel(,nogrid) ysc(off)  ///
                                         mlabcolor(red) xtitle("") 

graph save "ESS LIB", replace

/*MAIN TEXT: Figure3*/
twoway lpolyci cntryunique21 year if cntryrr==1, degree(3) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) alsty(nono) acolor(538rs9%50) level(95) fxsize(67) || lpolyci cntryunique20 year if cntryrr==1, degree(3)lcol(black) fcolor(black*.7) acolor(538rs9%50) alsty(nono) level(95) lwidth(thick) lpattern(solid) ytitle("")  || lpolyci cntryunique19 year if cntryrr==1,  degree(3) lcol(red)  alsty(nono)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("") caption(Source: ESS 1-10) graphregion(color(white)) ylabel(,nogrid) ysc(off) 
graph save "ESS LIB_simple" replace

/* Part 10: ESS panel for Rate of support for the green party family */
* Numerator: Number of manual workers M/F + general sample voting greens
gen mwmgrn=1 if (mnlwrk==1 & male==1 & prtyfam_grn==1)
gen mwfgrn=1 if (mnlwrk==1 & male==0 & prtyfam_grn==1)
by  year cntry, sort: egen nmwmgrn=total(mwmgrn)           if mwmgrn==1
by  year cntry, sort: egen nmwfgrn=total(mwfgrn)           if mwfgrn==1
by  year cntry, sort: egen ngrnv  =total(prtyfam_grn)      if prtyfam_grn==1

* Denominator: Number of manual workers M/F who voted and number of voters 
*  Created in Part 3: nmwmv, nmwfv, nv 

*Proportions
gen mwmgrn_mwmv   =nmwmgrn/nmwmv
gen mwfgrn_mwfv   =nmwfgrn/nmwfv
gen grnv_v        =ngrnv/nv

* generate % male manual workers per country voting GRN.*/
by essround cntry, sort: egen mwmcntrygrn=max(mwmgrn_mwmv)
egen cntryuniquemwmgrn=tag(mwmcntrygrn)
bys essround cntry:egen cntryunique22=sum(mwmcntrygrn) if cntryuniquemwmgrn==1
* generate % female manual workers per country voting GRN.*/
by essround cntry, sort: egen mwfcntrygrn=max(mwfgrn_mwfv)
egen cntryuniquemwfgrn=tag(mwfcntrygrn)
bys essround cntry:egen cntryunique23=sum(mwfcntrygrn) if cntryuniquemwfgrn==1
* generate % GRN voters per country*/
by essround cntry, sort: egen grnv=max(grnv_v)
egen cntryuniquergrn=tag(grnv)
bys essround cntry:egen cntryunique24=sum(grnv) if cntryuniquergrn==1

/*APPENDIX: Figure D4*/
twoway lpolyci cntryunique24 year if cntryrr==1, degree(2) alsty(nono)lcolor(gs8) lwidth(thick) fcolor(gs10*.7) acolor(538rs9%50) level(95)  ///
|| scatter grnv_v year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(gs8) ylab(, nogrid) fxsize(67) || ///										 
lpolyci cntryunique23 year if cntryrr==1, degree(2)lcol(black) alsty(nono)fcolor(black*.7) acolor(538rs9%50) level(95) lwidth(thick) lpattern(solid) ytitle("")  ///
|| scatter mwfgrn_mwfv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small) ///
                                         mlabcolor(black) xtitle("") ///
|| lpolyci cntryunique22 year if cntryrr==1,  degree(2) lcol(red) alsty(nono)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) ///
|| scatter mwmgrn_mwmv year if cntryrr==1, jitter(3) msymbol(i) mlabel(cntry) mlabsize(small)graphregion(color(white)) ylabel(,nogrid) ysc(off) caption(Source: ESS 1-10) ///
                                         mlabcolor(red) xtitle("") 

graph save "ESS grn", replace

/*MAIN TEXT: Figure3*/
twoway lpolyci cntryunique24 year if cntryrr==1, degree(2) lcolor(gs8) lwidth(thick) fcolor(gs10*.7) acolor(538rs9%50) alsty(nono) level(95) fxsize(67) || lpolyci cntryunique23 year if cntryrr==1, degree(2)lcol(black) fcolor(black*.7) acolor(538rs9%50)alsty(nono)  level(95) lwidth(thick) lpattern(solid) ytitle("")  || lpolyci cntryunique22 year if cntryrr==1,  degree(2) lcol(red)  alsty(nono)  fcolor(red*.7) acolor(538rs9%50) ///
level(95) lwidth(thick) lpattern(solid) legend(off) xtitle("")caption(Source: ESS 1-10 )  graphregion(color(white)) ylabel(,nogrid) ysc(off) 
graph save "ESS grn_simple" replace

/* Part 11: Combine the EB and ESS panels to form figure D3 in the Appendix*/
graph combine "EB 2a""ESS 2a", ycommon title((a))graphregion(color(white)) col(2) 
graph save "Figure 2a", replace

graph combine "EB 2b""ESS 2b", ycommon title((b)) col(2) graphregion(color(white))
graph save "Figure 2b", replace

graph combine "EB 2c""ESS 2c", ycommon title((c))col(2) graphregion(color(white))
graph save "Figure 2c", replace

graph combine "EB 2d""ESS 2d", ycommon col(2) graphregion(color(white))
graph save "Figure 2d", replace

*Figure D3 in the Appendix (combination of 4 graphs)
graph combine "Figure 2a" "Figure 2b" "Figure 2c" "Figure 2d", graphregion(color(white))
graph save "figure 2_combined", replace

* Figure 2 in the main text - combined simplified figure 2
graph combine "EB 2a_simple""ESS 2a_simple", ycommon graphregion(color(white))title ((a))col(2) 
graph save "Figure 2a_simple", replace

graph combine "EB 2b_simple" "ESS 2b_simple", ycommon graphregion(color(white))title ((b))col(2) 
graph save "Figure 2b_simple", replace

graph combine "EB 2c_simple""ESS 2c_simple", ycommon graphregion(color(white)) title ((c))col(2) 
graph save "Figure 2c_simple", replace

graph combine "EB 2d_simple""ESS 2d_simple", ycommon graphregion(color(white)) title ((d))col(2) 
graph save "Figure 2d_simple", replace

graph combine "Figure 2a_simple" "Figure 2b_simple" "Figure 2c_simple" "Figure 2d_simple", graphregion(color(white))
graph save "figure 2_combined_simple", replace

*Figure D4 in the Appendix*
graph combine "EB conservative""ESS conservative", ycommon col(2) graphregion(color(white))
graph save "cons support", replace

graph combine "EB CD" "ESS CD", ycommon col(2) graphregion(color(white))
graph save "CD support", replace

graph combine "EB RL" "ESS RL" , ycommon col(2) graphregion(color(white))
graph save "RL support", replace

graph combine "EB LIB" "ESS LIB" , ycommon col(2) graphregion(color(white))
graph save "LIB support", replace

graph combine "EB grn" "ESS grn" , ycommon col(2) graphregion(color(white))
graph save "GRN support", replace

*Figure 3 in the main text: simplified graphs*
graph combine "EB conservative_simple""ESS conservative_simple", ycommon title("Conservatives")col(2) graphregion(color(white))
graph save "cons support_simple", replace

graph combine "EB CD_simple""ESS CD_simple", ycommon title("Christian Democrats")col(2) graphregion(color(white))
graph save "cd support_simple", replace

graph combine "EB grn_simple""ESS grn_simple", ycommon title("Greens") col(2) graphregion(color(white))
graph save "green support_simple", replace

graph combine "EB LIB_simple""ESS LIB_simple", ycommon title("Liberals")col(2) graphregion(color(white))
graph save "LIB support_simple", replace

graph combine "EB RL_simple""ESS RL_simple", ycommon title("Radical Left")col(2) graphregion(color(white))
graph save "RL support_simple", replace

graph combine "RL support_simple" "cons support_simple" "cd support_simple" "LIB support_simple" "green support_simple" , col(2) graphregion(color(white)) 
graph save "Figure 3_simple", replace

/*TTEST REPOERTED IN THE PAPER*/
* RENAME AND SORT ATTITUDE CONTROLS: high value indicates anti-immigrant sentiment 
tab imdfetn
drop if imdfetn>4
rename imdfetn immdrace
label variable immdrace "Not allowing immigrants of a different race"
tab immdrace

tab impcntr
drop if impcntr>4
rename impcntr immpoor
label variable immpoor "Not allow immigrants from poorer countries outside Europe"
tab immpoor

tab imbgeco
drop if imbgeco>10 
recode imbgeco 0=10 1=9 2=8 3=7 4=6 5=5 6=4 ///
7=3 8=2 9=1 10=0 , gen(immbeco)
label define immbeco 0 "Good for the economy" ///
10 "Bad for the economy" , replace
label values immbeco immbeco 
tab immbeco

tab imwbcnt, nolabel
drop if imwbcnt>10
recode imwbcnt 0=10 1=9 2=8 3=7 4=6 5=5 6=4 ///
7=3 8=2 9=1 10=0 , gen(immcnt)
label define immcnt 0 "Immig's make country better" 10 "Immig's make country worse", replace
label values immcnt immcnt
tab immcnt 

tab imwbcnt, nolabel
drop if imueclt>10
recode imueclt 0=10 1=9 2=8 3=7 4=6 5=5 6=4 ///
7=3 8=2 9=1 10=0 , gen(immgclt)
label define immgclt 0 "Cultural life enriched" 10 "Cultural life undermined", replace
label values immgclt immgclt
tab immgclt 

* 1-DIGIT ISCO OCCUPATION 
gen  occupation=.   
replace occupation=1 if (iscoco<=1999&iscoco>=1000) | (isco08<=1999&isco08>=1000)
replace occupation=2 if (iscoco<=2999&iscoco>=2000) | (isco08<=2999&isco08>=2000)
replace occupation=3 if (iscoco<=3999&iscoco>=3000) | (isco08<=3999&isco08>=3000)
replace occupation=4 if (iscoco<=4999&iscoco>=4000) | (isco08<=4999&isco08>=4000)
replace occupation=5 if (iscoco<=5999&iscoco>=5000) | (isco08<=5999&isco08>=5000)
replace occupation=6 if (iscoco<=6999&iscoco>=6000) | (isco08<=6999&isco08>=6000)
replace occupation=7 if (iscoco<=7999&iscoco>=7000) | (isco08<=7999&isco08>=7000)
replace occupation=8 if (iscoco<=8999&iscoco>=8000) | (isco08<=8999&isco08>=8000)
replace occupation=9 if (iscoco<=9999&iscoco>=9000) | (isco08<=9999&isco08>=9000)
replace occupation=0 if iscoco==100 |  isco08<1000
label define occupation 1 " Legislators, senior officials and managers" ///
2 "Professionals" 3 " Technicians and associate professionals" 4 "Clerks " ///
5 "Service workers and shop and market sales workers" 6 "Skilled agricultural and fishery workers" ///
7 "Craft and related trades workers" 8 " Plant and machine operators and assemblers" ///
9 "Elementary occupations" 0 "Armed forces", replace
label values occupation occupation 

gen manual=.
replace manual=21.75 if occupation==1
replace manual=38.14 if occupation==2
replace manual=37.83 if occupation==3
replace manual=29.20 if occupation==4
replace manual=38.61 if occupation==5
replace manual=75.32 if occupation==7
replace manual=78.23 if occupation==8
replace manual=71.19 if occupation==9

gen communication=.
replace communication=85.45 if occupation==1
replace communication=69.46 if occupation==2
replace communication=67.42 if occupation==3
replace communication=70.18 if occupation==4
replace communication=64.46 if occupation==5
replace communication=18.10 if occupation==7
replace communication=21.11 if occupation==8
replace communication=29.86 if occupation==9

gen routine=.
replace routine=48.75 if occupation==1
replace routine=50.40 if occupation==2
replace routine=49.35 if occupation==3
replace routine=26.34 if occupation==4
replace routine=33.01 if occupation==5
replace routine=73.68 if occupation==7
replace routine=77.43 if occupation==8
replace routine=57.30 if occupation==9

pwcorr routine manual,sig /*reported in the paper, ft#2*/
pwcorr manual communication, sig/*reported in the paper*/

gen comm=communication/100
gen man=manual/100


gen manual_dum=.
replace manual_dum=1 if manual>70 & manual!=.
replace manual_dum=0 if manual<70 & manual!=.
tab manual_dum


/*volunarability and skills*/
ta jbscr /*job is secure*/
bys year: ta jbscr /*2004, 2010*/
recode jbscr 1=4 2=3 3=2 4=1 , gen(volnrblty)                

ta rpljbde /*How difficult/easy for employer to replace you if you left*/

ttest volnrblty, by(manual_dum)
ttest rpljbde, by(manual_dum)

log close 


